//
//  main.m
//  OBJC
//
//  Created by wangrui on 14-5-8.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <objc/objc-runtime.h>

@protocol SomeProtocal <NSObject>

@optional

+ (void)protocalClassMethod;

- (void)protocalInstanceMethod;

@end


@interface A : NSObject<SomeProtocal>

+ (void)classMethod;

- (void)instanceMethod;

@end


@implementation A

+ (void)classMethod {
    NSLog(@"class method");
}

- (void)instanceMethod {
    NSLog(@"instance method");
}


@end



int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        A *to = [[A alloc] init];
        [to description];
        
        
//        [to classMethod];//编译不过
//        [A instanceMethod];//编译不过
        
        
//        [to performSelector:@selector(classMethod)]; //运行挂
//        [A performSelector:@selector(instanceMethod)];//运行挂
        
        
        //为什么这两个能编译过呢，难道是因为是协议定义的方法？
        //类能响应协议的实例方法？试一试
        [A performSelector:@selector(classMethod)]; //运行过
        [A respondsToSelector:@selector(instanceMethod)];
        
        
        //[A protocalClassMethod]; 不实现会挂
        //[A protocalInstanceMethod]; //编译不过 上面的推测貌似是错的
        
        //runtime 里有同名的类方法，头文件里没有，难道编译器作了优化？
        
        NSLog(@"Class responseToSelector:%d",[NSObject respondsToSelector:@selector(respondsToSelector:)]);
        //NSLog(@"Class whatever a responseToSelector:%d",[NSObject respondsToSelector:@selector(whatEverSelector:)]); 输出0
        NSLog(@"Instance responseToSelector:%d",[NSObject instancesRespondToSelector:@selector(respondsToSelector:)]);
        
        
        //类respone
        NSLog(@"A response to sel:%d",[A respondsToSelector:@selector(classMethod)]);//outpute:1
        NSLog(@"A response to sel:%d",[A respondsToSelector:@selector(instanceMethod)]);//outpute:0
        
        //对象
        NSLog(@"A response to sel:%d",[to respondsToSelector:@selector(classMethod)]);//outpute:0
        NSLog(@"A response to sel:%d",[to respondsToSelector:@selector(instanceMethod)]);//outpute:1
        
        
        
        /*
         *查看一下源码
         *
         
         //=========================src begin =========================================
         + (BOOL)instancesRespondToSelector:(SEL)sel {
         if (!sel) return NO;
         return class_respondsToSelector(self, sel);
         }
         
         + (BOOL)respondsToSelector:(SEL)sel {
         if (!sel) return NO;
         return class_respondsToSelector(object_getClass((id)self), sel);
         }
         
         - (BOOL)respondsToSelector:(SEL)sel {
         if (!sel) return NO;
         return class_respondsToSelector([self class], sel);
         }
         //=========================src end=============================================
         
         *
         * (id)A
         * class 类型的指针 为什么可以强转 id呢。 因为objc_object和objc_class 结构体里面都只有
         * 一个class 指针,占的字节相同
         * 但是isa 指针指向的不是同一个东西了, 一个指向类，一个指向元类,所以强转后调用实例方法会崩溃
         *
         */
    }
    
    return 0;
}

